﻿import { Component, Input,Output,EventEmitter,OnChanges} from '@angular/core';

@Component({
    selector: 'custom-pager',
    templateUrl: './app/components/page/page.component.html'
})
export class PageComponent implements OnChanges {
    @Input() page: number;
    @Input() pagesCount: number;
    @Input() totalCount: number;
    @Output() searchEvent = new EventEmitter();
    range: any;
    constructor() {
    }
    ngOnChanges() {
        this.range = this.Range();
    }
    search(i: number) {
        console.log('search page:' + i);
        this.page = i;
        this.searchEvent.emit({ value: this.page });
    }


    Range() {
        if (!this.pagesCount) {
            return [];
        }
        console.log('range--' + this.pagesCount);
        let step = 2;
        let doubleStep = step * 2;
        let start = Math.max(0, this.page - step);
        let end = start + 1 + doubleStep;
        if (end > this.pagesCount) {
            end = this.pagesCount;
        }
        var ret = [];
        for (let i = start; i != end; ++i) {
            ret.push(i);
        }
        console.log('range--' + ret);
        return ret;
    }

    pagePlus(count: number) {
        return this.page + count;
    }
}
